package com.uptown.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author: Lixiaofei
 * @date: 2020/5/8 18:26
 */
public class NioFileChannel03 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel channel1 = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel channel2 = fileOutputStream.getChannel();
        ByteBuffer byteBuffer =  ByteBuffer.allocate(512);
        
        while (true){
            byteBuffer.clear();
            int read = channel1.read(byteBuffer);
            if(read == -1) {
                break;
            }
            byteBuffer.flip();
            channel2.write(byteBuffer);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}
